﻿

Public Class DataTier
    Dim serviceClient As New LQDataService.DataAccessSoapClient
    ''' <summary>
    ''' Create a new user
    ''' </summary>
    ''' <param name="newUserName">new user name</param>
    ''' <param name="newUserPass">new password</param>
    ''' <param name="email">new email address</param>
    ''' <returns>"200" successed, otherwise failed</returns>
    ''' <remarks></remarks>
    Public Function CreateUser(ByVal newUserName As String, ByVal newUserPass As String, ByVal email As String) As String
        Return serviceClient.CreateUser(newUserName, getMD5Hash(newUserPass), email)
    End Function
    ''' <summary>
    ''' Check if an username is already exist in the database
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <returns>true: exist,false: not exist</returns>
    ''' <remarks></remarks>
    Public Function IsExist(ByVal userName As String) As Boolean
        Return (serviceClient.IsExist(userName))
    End Function

    ''' <summary>
    ''' Check if the supplied username and password match the database record
    ''' </summary>
    ''' <param name="userName">username</param>
    ''' <param name="pass">password</param>
    ''' <returns>true: match, false: not match</returns>
    ''' <remarks></remarks>
    Public Function LoginCheck(ByVal userName As String, ByVal pass As String)
        Return serviceClient.LoginCheck(userName, getMD5Hash(pass))
    End Function

    ''' <summary>
    ''' Change password for a loged in user
    ''' </summary>
    ''' <param name="userName">username</param>
    ''' <param name="newPass">new passowrd</param>
    ''' <returns>'200' successful, otherwise failed</returns>
    ''' <remarks></remarks>
    Public Function ChangePassword(ByVal userName As String, ByVal newPass As String)
        Return serviceClient.ChangePassword(userName, getMD5Hash(newPass))
    End Function

    ''' <summary>
    ''' Upload an avatar to server and set it to user
    ''' </summary>
    ''' <param name="f">image file bytes</param>
    ''' <param name="userName">user</param>
    ''' <returns>'200' successful, otherwise failed</returns>
    ''' <remarks></remarks>
    Public Function ChangeAvatar(ByVal f As Byte(), ByVal userName As String)
        Return serviceClient.ChangeAvatar(f, userName)
    End Function
    ''' <summary>
    ''' Get the default avatar image address 
    ''' </summary>
    ''' <returns>image url</returns>
    ''' <remarks></remarks>
    Public Function GetDefaultAvatar()
        Return serviceClient.GetDefaultAvatar()
    End Function
    ''' <summary>
    ''' Load avatar image url for user
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <returns>image url</returns>
    ''' <remarks></remarks>
    Public Function LoadAvatar(ByVal userName As String)
        Return serviceClient.LoadAvatar(userName)
    End Function
    ''' <summary>
    ''' Change the best record for user
    ''' </summary>
    ''' <param name="time">time to finish maze</param>
    ''' <param name="userName"></param>
    ''' <returns>'200' success, otherwise failed</returns>
    ''' <remarks></remarks>
    Public Function NewBestRecord(ByVal time As String, ByVal userName As String)
        Return serviceClient.NewBestRecord(time, userName)
    End Function
    ''' <summary>
    ''' Get the best record for a user
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetBestRecord(ByVal userName As String)
        Return serviceClient.GetBestRecord_OneUser(userName)
    End Function
    ''' <summary>
    ''' Create a new game in the Game table
    ''' </summary>
    ''' <param name="userName">users that in this game</param>
    ''' <param name="game_id">game guid</param>
    ''' <param name="mazeData">maze json string</param>
    ''' <returns>'200' sucess</returns>
    ''' <remarks></remarks>
    Public Function CreateGame(ByVal userName As String,
                               ByVal game_id As String, ByVal mazeData As String) As String
        Return serviceClient.AddGameRecord(userName, game_id, mazeData)
    End Function
    ''' <summary>
    ''' Apend a new username in a game record's user column
    ''' </summary>
    ''' <param name="newPlayer">new player's name</param>
    ''' <param name="game_id">game id</param>
    ''' <returns>'200' success</returns>
    ''' <remarks></remarks>
    Public Function AddNewPlayerToGame(ByVal newPlayer As String, ByVal game_id As String) As String
        Return serviceClient.AddNewPlayerToGame(game_id, serviceClient.GetPlayerInGame(game_id) + "|" + newPlayer)
    End Function
    ''' <summary>
    ''' Delete a game from game table
    ''' </summary>
    ''' <param name="gameID">game id</param>
    ''' <returns>'200' success</returns>
    ''' <remarks></remarks>
    Public Function DeleteAFinishedGame(ByVal gameID As String) As String
        Return serviceClient.DeleteGame(gameID)
    End Function
    ''' <summary>
    ''' Get all players' name  that are in particularly game
    ''' </summary>
    ''' <param name="gameID">current game's id</param>
    ''' <returns>an string array that contain every player's name</returns>
    ''' <remarks></remarks>
    Public Function GetAllPlayersInGame(ByVal gameID As String) As String()
        Return serviceClient.GetPlayerInGame(gameID).Split("|")
    End Function
    ''' <summary>
    ''' Add the game id to user table's Game_ID column
    ''' </summary>
    ''' <param name="user">user name</param>
    ''' <param name="gameID">game id</param>
    ''' <returns>'200' success</returns>
    ''' <remarks></remarks>
    Public Function AddUserToGame(ByVal user As String, ByVal gameID As String) As String
        If serviceClient.CheckUserInGame(user) = "400" Then
            Return serviceClient.AddUserToGame(user, gameID)
        End If
        Return "User already in a game or something else is wrong"
    End Function
    ''' <summary>
    ''' Return an string array that contain all information associated with this user
    ''' </summary>
    ''' <param name="username"></param>
    ''' <returns>Return array contains element in following order<br/>
    ''' UserName,UserAvatar,UserEmail,User_BestRecord,Game_ID
    ''' </returns>
    ''' <remarks></remarks>
    Public Function GetAllUserData(ByVal username As String) As String()
        Dim res = serviceClient.GetAllUserData(username)
        Return res.ToArray()
    End Function
    ''' <summary>
    ''' Return X amount of user data that is sorted from lowest best record to highest
    ''' </summary>
    ''' <param name="user_num"></param>
    ''' <returns>return array contains element in folowing order<br/>
    ''' "User1","1","User2","2"......
    ''' </returns>
    ''' <remarks></remarks>
    Public Function GetTopXUsers(ByVal user_num As Int16) As TopRecord()
        Dim temp = serviceClient.GetBestScores_MultiUser(user_num).ToArray()
        Dim obj As New ArrayList
        For i = 0 To temp.Length - 1
            Dim tr As New TopRecord
            tr.userName = temp(i)
            tr.record = (Decimal.Parse(temp(i + 1)) / 1000)
            i = i + 1
            obj.Add(tr)
        Next
        Return obj.ToArray((GetType(TopRecord)))
    End Function
    ''' <summary>
    ''' Update user position data based on user id supplied
    ''' </summary>
    ''' <param name="playerId"></param>
    ''' <param name="pos"></param>
    ''' <param name="dir"></param>
    ''' <param name="gameID"></param>
    ''' <returns>'200' success</returns>
    ''' <remarks></remarks>
    Public Function StorePosOfPlayer(ByVal playerId As Int16, ByVal pos As String, ByVal dir As String, ByVal gameID As String) As String
        Return serviceClient.UpdatePlayerPosition(playerId, pos, dir, gameID)
    End Function
    ''' <summary>
    ''' Get user position and directin data based on id supplied
    ''' </summary>
    ''' <param name="gameid"></param>
    ''' <param name="playerID"></param>
    ''' <returns>["position data","direction data"]</returns>
    ''' <remarks></remarks>
    Public Function GetPlayerPos(ByVal gameid As String, ByVal playerID As Int16) As String()
        Return serviceClient.GetPlayerPosition(gameid, playerID).ToArray()
    End Function
    Public Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
        Dim bytesToHash() As Byte = Text.Encoding.ASCII.GetBytes(strToHash)
        bytesToHash = md5Obj.ComputeHash(bytesToHash)
        Return bytesToHash.Aggregate("", Function(current, b) current + b.ToString("x2"))
    End Function

End Class

